﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;

namespace ABELGX
{
    public class InfoCommon : CarInfo
    {
        /// <summary>
        /// 控制模式
        /// </summary>
        public ControlType Control { get; set; } = ControlType.Screen;
        /// <summary>
        /// 锁定状态
        /// </summary>
        public bool Lock { get; set; }
        /// <summary>
        /// 机械臂控制模式
        /// </summary>
        public ModelType Model { get; set; } = ModelType.Arm12;
        /// <summary>
        /// 行走/大臂 控制
        /// </summary>
        public bool ControlArm { get; set; }

        /// <summary>
        /// 主照明
        /// </summary>
        public RegisterBool MainLight { get; set; } = new RegisterBool();
        /// <summary>
        /// 喷淋
        /// </summary>
        public RegisterBool Spray { get; set; } = new RegisterBool();
        /// <summary>
        /// 告警
        /// </summary>
        public RegisterBool Warning { get; set; } = new RegisterBool();
        /// <summary>
        /// 避障
        /// </summary>
        public RegisterBool Avoidance { get; set; } = new RegisterBool();
        /// <summary>
        /// 刹车
        /// </summary>
        public RegisterBool Braking { get; set; } = new RegisterBool();
        /// <summary>
        /// 急停
        /// </summary>
        public RegisterBool EStop { get; set; } = new RegisterBool();
        /// <summary>
        /// 避障状态
        /// </summary>
        public RegisterBool AvoidanceHas {  get; set; } = new RegisterBool();
        /// <summary>
        /// 车速档位
        /// </summary>
        public int SpeedLevel { get; set; } = 1;
        /// <summary>
        /// 雾炮切换
        /// </summary>
        public WaterType WaterType { get; set; } = WaterType.Water;
        /// <summary>
        /// 报警音
        /// </summary>
        public int BeepLevel {  get; set; }
        /// <summary>
        /// 主控制
        /// </summary>
        public RegisterBool MainControl {  get; set; } = new RegisterBool();
        /// <summary>
        /// 辅助控制
        /// </summary>
        public RegisterBool SubControl {  get; set; } = new RegisterBool();
        /// <summary>
        /// 照明模式
        /// </summary>
        public RegisterBool LightType { get; set; } = new RegisterBool();
        /// <summary>
        /// 喷淋模式
        /// </summary>
        public RegisterBool SprayType { get; set; } = new RegisterBool(true);
        /// <summary>
        /// 气体开关
        /// </summary>
        public RegisterBool AirOpen { get; set; } = new RegisterBool(true);
        /// <summary>
        /// 驱动复位
        /// </summary>
        public RegisterBool DriverReset { get; set; } = new RegisterBool();
        /// <summary>
        /// 马达同步
        /// </summary>
        public RegisterBool MotorSync { get; set; } = new RegisterBool(true);
        /// <summary>
        /// 蜂鸣器
        /// </summary>
        public RegisterBool Beep { get; set; } = new RegisterBool(true);
        /// <summary>
        /// 发电机
        /// </summary>
        public RegisterBool Generator { get; set; } = new RegisterBool();
        /// <summary>
        /// 高音喇叭
        /// </summary>
        public RegisterBool HighSpeaker { get; set; } = new RegisterBool();
        /// <summary>
        /// 水带脱扣
        /// </summary>
        public RegisterBool[] WaterHose { get; set; } = new RegisterBool[8];
        /// <summary>
        /// 发动机启动
        /// </summary>
        public RegisterBool MotorOn { get; set; } = new RegisterBool();
        /// <summary>
        /// 发动机熄火
        /// </summary>
        public RegisterBool MotorOff { get; set; } = new RegisterBool();
        /// <summary>
        /// 油门+
        /// </summary>
        public RegisterBool AccUp { get; set; } = new RegisterBool();
        /// <summary>
        /// 油门-
        /// </summary>
        public RegisterBool AccDown { get; set; } = new RegisterBool();
        /// <summary>
        /// 风扇速度+
        /// </summary>
        public RegisterBool FanSpeedUp { get; set; } = new RegisterBool();
        /// <summary>
        /// 风扇速度-
        /// </summary>
        public RegisterBool FanSpeedDown { get; set; } = new RegisterBool();
        /// <summary>
        /// 风机仰
        /// </summary>
        public RegisterBool FanUp { get; set; } = new RegisterBool();
        /// <summary>
        /// 风机俯
        /// </summary>
        public RegisterBool FanDown { get; set; } = new RegisterBool();
        /// <summary>
        /// 风机正转
        /// </summary>
        public RegisterBool FanPositive { get; set; } = new RegisterBool();
        /// <summary>
        /// 风机反转
        /// </summary>
        public RegisterBool FanNegative { get; set; } = new RegisterBool();
        /// <summary>
        /// 臂架起
        /// </summary>
        public RegisterBool ArmUp { get; set; } = new RegisterBool();
        /// <summary>
        /// 臂架落
        /// </summary>
        public RegisterBool ArmDown { get; set; } = new RegisterBool();
        /// <summary>
        /// 推铲起
        /// </summary>
        public RegisterBool DozerUp { get; set; } = new RegisterBool();
        /// <summary>
        /// 推铲落
        /// </summary>
        public RegisterBool DozerDown { get; set; } = new RegisterBool();
        /// <summary>
        /// 自定义
        /// </summary>
        public RegisterBool[] Customize { get; set; } = new RegisterBool[16];
        public InfoCommon(List<CustomizeSet> customizeSets,params byte[] func) : base(func)
        {
            for (int i = 0; i < WaterHose.Length; i++)
            {
                WaterHose[i] = new RegisterBool();
            }
            for (int i = 0; i < Customize.Length; i++)
            {
                Customize[i] = new RegisterBool(customizeSets.Find(x => x.Index == i), false);
            }
        }
        public override void ProcessData(byte[] data, int start)
        {
            int i = start + 1;
            switch (data[start])
            {
                case INFO_COMMON:
                    switch (data[i] & 0x03)
                    {
                        case 1:
                            
                            break;
                        case 2:
                            Control = ControlType.Screen;
                            break;
                        case 0:
                            Control = ControlType.SBUS;
                            break;
                    }
                    Lock = (data[i] & 0x04) != 0;
                    Braking.Value = (data[i] & 0x08) != 0;
                    switch ((data[i] >> 5) & 0x03)
                    {
                        case 0:
                            Model = ModelType.Arm12;
                            break;
                        case 1:
                            Model = ModelType.UnderWater;
                            break;
                        case 2:
                            Model = ModelType.Arm3Wrist;
                            break;
                    }
                    ControlArm= (data[i] & 0x80) != 0;
                    i++;

                    MainLight.Value = (data[i] & 0x01) != 0;
                    Spray.Value = (data[i] & 0x02) != 0;
                    Warning.Value = (data[i] & 0x04) != 0;
                    Avoidance.Value = (data[i] & 0x08) != 0;
                    EStop.Value = (data[i] & 0x10) != 0;
                    AvoidanceHas.Value = (data[i] & 0x20) != 0;
                    i++;

                    SpeedLevel = data[i] & 0x03;
                    switch ((data[i] >> 2) & 0x03)
                    {
                        case 0:
                            WaterType = WaterType.Water;
                            break;
                        case 2:
                            WaterType = WaterType.All;
                            break;
                        case 1:
                            WaterType = WaterType.Fog;
                            break;
                    }
                    BeepLevel = (data[i] & 0x30) >> 4;
                    MainControl.Value = (data[i] & 0x40) != 0;
                    SubControl.Value = (data[i] & 0x80) != 0;
                    i++;

                    LightType.Value = (data[i] & 0x01) != 0;
                    SprayType.Value = (data[i] & 0x02) != 0;
                    AirOpen.Value = (data[i] & 0x04) != 0;
                    DriverReset.Value = (data[i] & 0x08) != 0;
                    MotorSync.Value = (data[i] & 0x10) != 0;
                    Beep.Value = (data[i] & 0x20) != 0;
                    Generator.Value = (data[i] & 0x40) != 0;
                    HighSpeaker.Value = (data[i] & 0x80) != 0;
                    i++;

                    WaterHose[0].Value = (data[i] & 0x01) != 0;
                    WaterHose[1].Value = (data[i] & 0x02) != 0;
                    WaterHose[2].Value = (data[i] & 0x04) != 0;
                    WaterHose[3].Value = (data[i] & 0x08) != 0;
                    WaterHose[4].Value = (data[i] & 0x10) != 0;
                    WaterHose[5].Value = (data[i] & 0x20) != 0;
                    WaterHose[6].Value = (data[i] & 0x40) != 0;
                    WaterHose[7].Value = (data[i] & 0x80) != 0;
                    i++;

                    FanPositive.Value = (data[i] & 0x01) != 0;
                    FanNegative.Value = (data[i] & 0x02) != 0;
                    i++;

                    Customize[0].Value = (data[i] & 0x01) != 0;
                    Customize[1].Value = (data[i] & 0x02) != 0;
                    Customize[2].Value = (data[i] & 0x04) != 0;
                    Customize[3].Value = (data[i] & 0x08) != 0;
                    Customize[4].Value = (data[i] & 0x10) != 0;
                    Customize[5].Value = (data[i] & 0x20) != 0;
                    Customize[6].Value = (data[i] & 0x40) != 0;
                    Customize[7].Value = (data[i] & 0x80) != 0;
                    i++;

                    Customize[8].Value = (data[i] & 0x01) != 0;
                    Customize[9].Value = (data[i] & 0x02) != 0;
                    Customize[10].Value = (data[i] & 0x04) != 0;
                    Customize[11].Value = (data[i] & 0x08) != 0;
                    Customize[12].Value = (data[i] & 0x10) != 0;
                    Customize[13].Value = (data[i] & 0x20) != 0;
                    Customize[14].Value = (data[i] & 0x40) != 0;
                    Customize[15].Value = (data[i] & 0x80) != 0;
                    i++;
                    break;
                default:
                    break;
            }
        }

        public override void InitData()
        {
            
        }
    }
}
